package com.kkd.kkdapp.activity;

import java.text.DecimalFormat;
import java.util.List;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.EverydayDealDetailsAdapter;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.EverydayDealData;
import com.kkd.kkdapp.http.entity.EverydayDealData1;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.PreferenceUtils;
import com.kkd.kkdapp.utils.SystemUtils;
import com.kkd.kkdapp.utils.Time_Date;

import com.kkd.kkdapp.view.KkdXlistView;
import com.kkd.kkdapp.view.KkdXlistView.KKDScrollListener;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

/**
 * 天天赚明细
 * 
 * @author mty
 * 
 */
public class EverydayDealDetailsActivity extends BaseActivity implements
		OnClickListener, KKDScrollListener {
	private Context mContext = this;
	private RelativeLayout mReturn;
	private KkdXlistView mXListView;
	private int pageNow = 1;
	private int pageSize = 10;
	private TextView textView_num;
	// private String version;
	private List<EverydayDealData1> data1;
	private EverydayDealDetailsAdapter mEverydayDealDetailsAdapter;
	private ImageView id_no_content;
	private TextView id_no_content_text;
	private boolean REFRESHFLAG = true;
	private Time_Date time_Date = new Time_Date();;
	private String yuebaoBalance;
	private DecimalFormat df = new DecimalFormat("#,###.00");
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			String time = time_Date.get_Year_Day();
			switch (msg.what) {
			case 1:
				mXListView.stopLoadMore(-1);
				break;
			case 2:
				mXListView.setNoMore(false);
				if (mEverydayDealDetailsAdapter == null) {
					mEverydayDealDetailsAdapter = new EverydayDealDetailsAdapter(
							mContext);
					mEverydayDealDetailsAdapter.appendData(data1, pageNow == 1);
					mListView.setAdapter(mEverydayDealDetailsAdapter);
				} else {
					mEverydayDealDetailsAdapter.appendData(data1, pageNow == 1);
					mEverydayDealDetailsAdapter.notifyDataSetChanged();
				}
				if (data1.size() < pageSize) {
					mXListView.stopLoadMore(1);
				} else {
					mXListView.stopLoadMore(0);
				}
				mXListView.setRefreshTime(time);
				break;
			case 3:
				textView_num.setText(yuebaoBalance);
				mXListView.stopRefresh();
				mXListView.setRefreshTime(time);
				if (REFRESHFLAG) {
					if (data1 == null) {
						// mXListView.setPullLoadEnable(false);
						id_no_content.setVisibility(View.VISIBLE);
						id_no_content_text.setVisibility(View.VISIBLE);
					} else {
						// mXListView.setPullLoadEnable(true);
						id_no_content.setVisibility(View.GONE);
						id_no_content_text.setVisibility(View.GONE);
					}
				}
				break;
			case 4:
				mXListView.stopLoadMore(1);
				mXListView.stopRefresh();
				String desc = (String) msg.obj;
				showToast(desc);
				break;
			default:
				break;
			}
		};
	};
	private ListView mListView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_everyday_deal_details);
		initView();
		setListener();
		getYuebaoTradeWaterList();
	}

	public void initView() {
		mReturn = (RelativeLayout) this.findViewById(R.id.everydaty_img);
		mXListView = (KkdXlistView) this.findViewById(R.id.everyday_lv);
		id_no_content = (ImageView) this.findViewById(R.id.id_no_content);
		id_no_content_text = (TextView) this
				.findViewById(R.id.id_no_content_text);

		mXListView.setKKDScrollListener(this);
		mListView = mXListView.mListView;
		mListView.setPadding(0, 0, 0, 0);
		mListView.setDividerHeight(0);

		// head
		RelativeLayout headViewContainer = new RelativeLayout(mContext);
		headViewContainer.setGravity(Gravity.CENTER_VERTICAL);
		headViewContainer
				.setLayoutParams(new android.widget.AbsListView.LayoutParams(
						android.widget.AbsListView.LayoutParams.MATCH_PARENT,
						android.widget.AbsListView.LayoutParams.WRAP_CONTENT));
		headViewContainer.setPadding(0, 0, 0, SystemUtils.dip2px(mContext, 15));
		headViewContainer.setBackgroundColor(Color.parseColor("#F0EEF3"));
		RelativeLayout headView = new RelativeLayout(mContext);
		headView.setGravity(Gravity.CENTER_VERTICAL);
		headView.setLayoutParams(new android.widget.AbsListView.LayoutParams(
				android.widget.AbsListView.LayoutParams.MATCH_PARENT,
				android.widget.AbsListView.LayoutParams.WRAP_CONTENT));
		headView.setPadding(SystemUtils.dip2px(mContext, 15),
				SystemUtils.dip2px(mContext, 10),
				SystemUtils.dip2px(mContext, 15),
				SystemUtils.dip2px(mContext, 10));
		headView.setBackgroundColor(Color.WHITE);
		// *******************textview可用特权本金************************
		TextView textView_title = new TextView(mContext);
		LayoutParams param0 = new android.widget.RelativeLayout.LayoutParams(
				android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
				android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
		param0.addRule(RelativeLayout.CENTER_VERTICAL);
		textView_title.setLayoutParams(param0);
		textView_title.setText("天天赚总额(元)");
		textView_title.setTextColor(Color.parseColor("#A9A9A9"));
		headView.addView(textView_title);
		// *******************textview可用特权本金金额************************
		textView_num = new TextView(mContext);
		android.widget.RelativeLayout.LayoutParams params = new android.widget.RelativeLayout.LayoutParams(
				android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
				android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
		params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		textView_num.setLayoutParams(params);
		textView_num.setText("2222");
		textView_num.setTextSize(sp(8));
		textView_num.setTextColor(Color.parseColor("#FF7301"));
		headView.addView(textView_num);
		headViewContainer.addView(headView);
		// listview
		mListView.addHeaderView(headViewContainer);

		mXListView.startRefresh();
	}

	public void setListener() {
		mReturn.setOnClickListener(this);
		// mXListView.setXListViewListener(this);
		// mXListView.setPullRefreshEnable(true);
		if (AppUtil.isConnect(this)) {
			// mXListView.setPullLoadEnable(true);
			id_no_content.setVisibility(View.GONE);
			id_no_content_text.setVisibility(View.GONE);
		} else {
			// mXListView.setPullLoadEnable(false);
			id_no_content.setVisibility(View.VISIBLE);
			id_no_content_text.setVisibility(View.VISIBLE);
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.everydaty_img:
			finish();
			break;
		}
	}

	/**
	 * 天天赚明细
	 */
	public void getYuebaoTradeWaterList() {
		KkdHttpMagager.getInstance().getEverydayDetailsManager( pageNow,
				pageSize,
				EverydayDealData.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {

						EverydayDealData mEverydayDealData = (EverydayDealData) responseBody;
						yuebaoBalance = df.format(mEverydayDealData
								.getYuebaoBalance() / 100f);
						if (yuebaoBalance.contains(".")) {
							if (yuebaoBalance.startsWith(".")) {
								yuebaoBalance = "0" + yuebaoBalance;
							}
						}
						int pagerNO = mEverydayDealData.getPageNO();
						int pageCount = mEverydayDealData.getPageCount();
						if (pagerNO > pageCount) {
							handler.sendEmptyMessage(1);
						} else {
							data1 = mEverydayDealData.getData1();
							handler.sendEmptyMessage(2);
						}
						handler.sendEmptyMessage(3);

					}

					@Override
					public void onFailure(int statusCode, String desc) {
						Message msg = new Message();
						msg.what = 4;
						msg.obj = desc;
						handler.sendMessage(msg);
					}
				});
	}

	@Override
	public void onRefresh() {
		pageNow = 1;
		getYuebaoTradeWaterList();
		REFRESHFLAG = true;
	}

	@Override
	public void onLoadMore() {
		pageNow++;
		getYuebaoTradeWaterList();
		REFRESHFLAG = false;
	}

	private float sp(final int sp) {
		float scaledDensity = getResources().getDisplayMetrics().scaledDensity;
		return sp * scaledDensity;
	}
	// @Override
	// public void onRefresh() {
	// pageNow = 1;
	// getYuebaoTradeWaterList();
	// REFRESHFLAG = true;
	// }
	//
	// @Override
	// public void onLoadMore() {
	// pageNow++;
	// getYuebaoTradeWaterList();
	// REFRESHFLAG = false;
	// }
}
